package com.ysd.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.DesignerInfo;
import com.ysd.entity.Detailsinfo;
import com.ysd.util.DBManager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DetailsinfoDaoimpl {
	public List<Detailsinfo> Detailsinfo(int did){
		List<Detailsinfo> list = new ArrayList<Detailsinfo>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			
			rs = st.executeQuery("select * from Detailsinfo where consultationId="+did);
			while(rs.next()) {
				int newsId = rs.getInt("newsId");
				String newsTitle = rs.getString("newsTitle");
				String newsDetails = rs.getString("newsDetails");
				String nt = rs.getString("nt");
				int consultationId = rs.getInt("consultationId");
				Detailsinfo  Detailsinfo = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId,nt);
				list.add(Detailsinfo);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	/**
	*多条件查询
	*
	**/
	public List<Detailsinfo> getDetailsinfo(Detailsinfo p){
		List<Detailsinfo>list = new ArrayList<Detailsinfo>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select p.*,pc.classification from detailsinfo p, newstable pc where 1=1";
			if (p.getNewsId()>0 ) {
				sql +=" and newsId="+p.getNewsId();//id查询
			}
			if (p.getNewsTitle()!=null && !p.getNewsTitle().equals("")) {//名字查询
				sql +=" and NewsTitle like '%"+p.getNewsTitle()+"%'";
			}
			if (p.getConsultationId()>0 ) {
				sql +=" and consultationId="+p.getConsultationId();//id查询
			}
			sql+=" and p.consultationId=pc.newsConsultationId";
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int newsId = rs.getInt("newsId");
				String newsTitle = rs.getString("newsTitle");
				String newsDetails = rs.getString("newsDetails");				
				String nt = rs.getString("nt");
				int consultationId = rs.getInt("consultationId");
				String classification = rs.getString("classification");
				Detailsinfo  Detailsinfo = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt, classification);
				list.add(Detailsinfo);
			}
			return list;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	/*多条件分页查询
		*
		**/
	public List<Detailsinfo> getDetailsinfoByPage(Detailsinfo p,int page ,int size){
		List<Detailsinfo>list = new ArrayList<Detailsinfo>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select p.*,pc.classification from detailsinfo p, newstable pc where 1=1";
			if (p.getNewsId()>0 ) {
				sql +=" and newsId="+p.getNewsId();//id查询
			}
			if (p.getNewsTitle()!=null && !p.getNewsTitle().equals("")) {//名字查询
				sql +=" and NewsTitle like '%"+p.getNewsTitle()+"%'";
			}
			if (p.getConsultationId()>0 ) {
				sql +=" and consultationId="+p.getConsultationId();//id查询
			}
			sql+=" and p.consultationId=pc.newsConsultationId limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int newsId = rs.getInt("newsId");
				String newsTitle = rs.getString("newsTitle");
				String newsDetails = rs.getString("newsDetails");				
				String nt = rs.getString("nt");
				int consultationId = rs.getInt("consultationId");
				String classification = rs.getString("classification");
				Detailsinfo  Detailsinfo = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt, classification);
				list.add(Detailsinfo);
			}
			return list;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	public Detailsinfo getDetailsinfoById(int cateId) {
		Detailsinfo pc = null;
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs= st.executeQuery("select * from Detailsinfo where newsId="+cateId);
			while (rs.next()) {
				int newsId = rs.getInt(1);
				String newsTitle = rs.getString(2);
				String newsDetails = rs.getString(3);
				int consultationId = rs.getInt(4);
				String nt = rs.getString(5);
				pc = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt);
			}
			return pc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int updateDetailsinfo(Detailsinfo pc) {
		return DBManager.executeUpdate("update Detailsinfo set newsTitle='"+pc.getNewsTitle()+"',nt='"+pc.getNt()+"' ,newsDetails='"+pc.getNewsDetails()+"' ,consultationId='"+pc.getConsultationId()+"' where newsId="+pc.getNewsId());
	}
}
